package com.duke.shaking.widget;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;

import com.androidquery.AQuery;
import com.duke.shaking.R;
import com.duke.shaking.pojo.UserImage;

public class GalleryAdapter extends BaseAdapter {
	
	private static final String TAG = "GalleryAdapter";
	private Context context;
    private LayoutInflater mInflater;
    private AQuery aQuery;
    private List<UserImage> imageList;
    
	public GalleryAdapter(Context mContext, List<UserImage> imageList) {
		super();
		this.context = mContext;
		mInflater = LayoutInflater.from(context);
		aQuery = new AQuery(context);
		this.imageList = imageList;
	}

	public int getCount() {
		return imageList.size();
	}

	public Object getItem(int position) {
		return imageList.get(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
        
        Log.i(TAG, "in getView for position " + position + 
        		", convertView is " +
        		((convertView == null)?"null":"being recycled"));

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.view_photo_in_gallary, null);
            convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.gridImageView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //holder.image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        //holder.image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        aQuery.id(holder.image).image(
        		((UserImage)getItem(position)).getUrl(), false, true);

        return convertView;

        /*ImageView imageView = new ImageView(context);
		imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		aQuery.id(imageView).image(((UserImage)getItem(position)).getUrl(), true, true);
		return imageView;*/
	}
	
	static class ViewHolder {
        ImageView image;
    }

	public long getItemId(int position) {
		
		// TODO Auto-generated method stub
		return 0;
	}
}
